package cn.shyouchi.wuguanjia.android.ui.patrol.selline;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.api.patrol.PatrolApi;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.event.PatrolLineSelEvent;
import cn.shyouchi.wuguanjia.android.impl.ModuleItemClickListener;
import cn.shyouchi.wuguanjia.android.ui.event.checknum.CheckNumLineEntity;
import cn.shyouchi.wuguanjia.android.widget.TopBar;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class SelPatrolLineActivity extends BaseActivity implements ModuleItemClickListener<CheckNumLineEntity.ResultMessageBean> {

    @BindView(R.id.topBar)
    TopBar mTopBar;
    @BindView(R.id.rv_line)
    RecyclerView mRvLine;


    PatrolLineAdapter mPatrolLineAdapter;

    List<CheckNumLineEntity.ResultMessageBean> mPatrolLineEntities = new ArrayList<>();

    PatrolApi mPatrolApi;

    @Override
    public int initContentView() {
        return R.layout.activity_sel_patrol_line;
    }

    @Override
    public void initInjector() {
        mPatrolApi = new PatrolApi(mContext);

    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.setTitle(getTitle().toString());
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        setSupportActionBar(mTopBar.getToolbar());
        mPatrolLineAdapter = new PatrolLineAdapter(mPatrolLineEntities, this);
        mRvLine.setLayoutManager(new LinearLayoutManager(mContext));
        mRvLine.setAdapter(mPatrolLineAdapter);
        fetchLine();

    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }


    @Override
    public void moduleItemClick(CheckNumLineEntity.ResultMessageBean model) {
        EventBus.getDefault().post(new PatrolLineSelEvent(model));
        finish();
    }

    @Override
    public void moduleItemLongClick(CheckNumLineEntity.ResultMessageBean model) {

    }

    @Override
    public void onDel(CheckNumLineEntity.ResultMessageBean model) {

    }

    private void fetchLine() {
        mPatrolApi.getPatrolLines()
                .subscribeOn(Schedulers.io())
                .map(new Func1<CheckNumLineEntity, List<CheckNumLineEntity.ResultMessageBean>>() {
                    @Override
                    public List<CheckNumLineEntity.ResultMessageBean> call(CheckNumLineEntity entity) {
                        return entity.getResultMessage();
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<CheckNumLineEntity.ResultMessageBean>>() {
                    @Override
                    public void onCompleted() {
                        mPatrolLineAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(List<CheckNumLineEntity.ResultMessageBean> resultMessageBeen) {
                        mPatrolLineEntities.addAll(resultMessageBeen);
                    }
                });
    }
}
